Option Strict Off
Option Explicit On

Imports System.IO

Imports Microsoft.Win32

Namespace SDIFChecker

    Public Module Global_Renamed
        Public Const dbText As Short = 10
        Public Const dbInteger As Short = 3
        Public gcSDIFCHECK As String = "SDIFChecker"
        Public gsREGSDIFCHECK As String = "Software\" & My.Application.Info.CompanyName
        Public gsSDIFName As String
        Public gsVersion As String
        Public gsSettings() As Byte
        Public gbSettings As Boolean
        Public giFileNumber As Short
        Public gsOutputLog As String
        Public gsString As String
        Public gbShowRegAtStartup As Boolean

        Function IsaDate(ByRef tDate As String) As Boolean

            Try
                If Len(tDate) <> 8 Or IsNumeric(tDate) = False Then
                    IsaDate = False
                Else
                    Dim tStr As String = Left(tDate, 2) & "/" & Mid(tDate, 3, 2) & "/" & Right(tDate, 4)
                    IsaDate = IsDate(tStr)
                End If
            Catch ex As Exception
                IsaDate = False
            End Try

        End Function

        ' center a window
        Sub CenterMe(ByRef rfrm As Object, ByRef rwScreenMDI As Short)
            Try
                If rwScreenMDI = gnSCREEN Then
                    rfrm.Top = (VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) - rfrm.Height) \ 2
                    rfrm.Left = (VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) - rfrm.Width) \ 2
                Else
                    If rfrm.MDIChild = True Then
                        rfrm.Top = ((VB6.PixelsToTwipsY(frmMain.Height) - rfrm.Height) \ 2) - 800
                        rfrm.Left = (VB6.PixelsToTwipsX(frmMain.Width) - rfrm.Width) \ 2
                    Else
                        rfrm.Top = VB6.PixelsToTwipsY(frmMain.Top) + (VB6.PixelsToTwipsY(frmMain.Height) - rfrm.Height) \ 2
                        rfrm.Left = VB6.PixelsToTwipsX(frmMain.Left) + (VB6.PixelsToTwipsX(frmMain.Width) - rfrm.Width) \ 2
                    End If
                End If
            Catch ex As Exception

            End Try
        End Sub

        Function Max(ByRef a As Short, ByRef b As Short) As Single

            If a > b Then
                Max = a
            Else
                Max = b
            End If

        End Function

        Function Min(ByRef a As Short, ByRef b As Short) As Single

            If a > b Then
                Min = b
            Else
                Min = a
            End If

        End Function

        '------------------------------------------------------------
        'saves current SDIFCHECK values in SDIFCHECK.INI
        '------------------------------------------------------------
        Sub SaveINISettings()

            Dim I As Short

            Try
                For I = 1 To 4
                    If frmMain.mnuSDIF(I).Visible Then
                        RegSaveSetting(gsREGSDIFCHECK, gcSDIFCHECK, "MRUDatabase" & I, Mid(frmMain.mnuSDIF(I).Text, 4, Len(frmMain.mnuSDIF(I).Text)))
                        RegSaveSetting(gsREGSDIFCHECK, gcSDIFCHECK, "MRUConnect" & I, frmMain.mnuSDIF(I).Tag)
                    Else
                        RegSaveSetting(gsREGSDIFCHECK, gcSDIFCHECK, "MRUDatabase" & I, "")
                        RegSaveSetting(gsREGSDIFCHECK, gcSDIFCHECK, "MRUConnect" & I, "")
                    End If
                Next
            Catch ex As Exception

            End Try


        End Sub

        '------------------------------------------------------------
        'this sub adds the just opened database to the most recently
        'used list in the File menu
        '------------------------------------------------------------
        Sub AddMRU(ByRef tStr As String)
            On Error GoTo AMErr

            Dim I, J As Short

            '1st look to see if it alread exists and swap it if it does
            For I = 1 To 4

                If UCase(Mid(frmMain.mnuSDIF(I).Text, 4, Len(frmMain.mnuSDIF(I).Text))) = UCase(tStr) Then
                    For J = I To 2 Step -1
                        frmMain.mnuSDIF(J).Text = "&" & J & " " & Mid(frmMain.mnuSDIF(J - 1).Text, 4, Len(frmMain.mnuSDIF(J - 1).Text))
                        frmMain.mnuSDIF(J).Tag = frmMain.mnuSDIF(J - 1).Tag
                    Next
                    GoTo Finish
                End If
            Next

            'wasn't there so move everything down one
            For I = 3 To 1 Step -1
                frmMain.mnuSDIF(I + 1).Text = "&" & I + 1 & " " & Mid(frmMain.mnuSDIF(I).Text, 4, Len(frmMain.mnuSDIF(I).Text))
                frmMain.mnuSDIF(I + 1).Tag = frmMain.mnuSDIF(I).Tag
            Next

Finish:
            frmMain.mnuSDIF(1).Text = "&1 " & tStr
            frmMain.mnuFileDash1.Visible = True
            For I = 1 To 4
                If Len(Mid(frmMain.mnuSDIF(I).Text, 4, Len(frmMain.mnuSDIF(I).Text))) > 0 Then
                    frmMain.mnuSDIF(I).Visible = True
                End If
            Next

            Exit Sub

AMErr:

        End Sub

        '------------------------------------------------------------
        'this sub sets the HourGlass icon for the mouse
        '------------------------------------------------------------
        Sub SetHourglass()
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
        End Sub

        Sub UnSetHourglass()
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
        End Sub

        Function MakeTemp(ByRef tStr As String) As String
            Dim l As Object
            Dim trev As String = ""

            Try
                MakeTemp = ""
                Dim tbFirst As Boolean = True
                For l = Len(tStr) To 1 Step -1
                    If Mid(tStr, l, 1) = "\" And tbFirst = True Then
                        trev = trev & "~"
                        tbFirst = False
                    End If
                    trev = trev & Mid(tStr, l, 1)
                Next l

                For l = Len(trev) To 1 Step -1
                    MakeTemp = MakeTemp & Mid(trev, l, 1)
                Next l
            Catch ex As Exception
                MakeTemp = ""
            End Try

        End Function

        Function GetPath(ByRef tStr As String) As String
            Dim I As Short
            Dim l As Object
            Dim trev As String = ""
            Dim tTmp As String = ""

            GetPath = ""

            Try
                If tStr Is System.DBNull.Value.ToString Or Len(tStr) = 0 Then
                    tTmp = RegGetSetting(SDIFChecker.gsREGSDIFCHECK, SDIFChecker.gcSDIFCHECK, "DialogPath", "")
                    If Len(tTmp) = 0 Then
                        GetPath = My.Application.Info.DirectoryPath
                    Else
                        GetPath = tTmp
                    End If
                Else
                    ' Reverse String
                    For l = Len(tStr) To 1 Step -1
                        trev = trev & Mid(tStr, l, 1)
                    Next l
                    I = InStr(1, trev, "\", 1)
                    tTmp = Mid(trev, I, Len(tStr) - I + 1)
                    For l = Len(tTmp) To 1 Step -1
                        GetPath = GetPath & Mid(tTmp, l, 1)
                    Next l
                End If
            Catch ex As Exception
                GetPath = ""
            End Try

        End Function

        Function SetPath(ByRef tStr As String) As Object

            SetPath = GetPath(tStr)
            RegSaveSetting(gsREGSDIFCHECK, gcSDIFCHECK, "DialogPath", SetPath)

        End Function

#Region "Registry"

        Function RegGetSetting(ByVal AppName As String, ByVal Section As String, ByVal Key As String, ByVal vsDefault As String) As String
            Dim regKey As RegistryKey

            Try
                regKey = OpenRegistrySubKey(Path.Combine(AppName, Section), False)
                RegGetSetting = regKey.GetValue(Key)
                If RegGetSetting Is Nothing Then
                    RegGetSetting = vsDefault
                End If
                regKey.Close()
            Catch ex As Exception
                RegGetSetting = vsDefault
            End Try
        End Function

        Sub RegSaveSetting(ByVal AppName As String, ByVal Section As String, ByVal Key As String, ByVal Setting As String)
            Dim regKey As RegistryKey

            Try
                regKey = OpenRegistrySubKey(Path.Combine(AppName, Section), True)
                regKey.SetValue(Key, Setting)
                regKey.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub

        Private Function OpenRegistrySubKey(ByVal tKey As String, ByVal tFlag As Boolean) As RegistryKey
            OpenRegistrySubKey = Registry.CurrentUser.OpenSubKey(tKey, tFlag)
            If OpenRegistrySubKey Is Nothing Then
                Registry.CurrentUser.CreateSubKey(tKey)
                OpenRegistrySubKey = Registry.CurrentUser.OpenSubKey(tKey, tFlag)
            End If
        End Function

#End Region
    End Module

End Namespace